﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AAPL.Common
{
    public class RuleError : IComparable
    {
        /// <summary>
        /// Optional. Name of the class field that 
        /// failed the rule. Used mainly for validation.
        /// </summary>
        public string FieldName { get; set; }


        /// <summary>
        /// Optional. UI Name that corresponds to the FieldName. 
        /// This allows us to use a UI friendly name in the ErrorMessage 
        /// if the UI uses a different name than the class.
        /// Used mainly for validation.
        /// </summary>
        public string UIFieldName { get; set; }


        /// <summary>
        /// Optional. Can be set by the application code to
        /// force a specific sort order on a list of errors.
        /// </summary>
        public int SortOrder { get; set; }  



        /// <summary>
        /// The error message that is passed back to the app.
        /// Contains the <FieldName> marker instead of field names.  
        /// This allows us to replace with a UI-friendly name, the 
        /// UIFieldName.  If UIFieldName isn't set use FieldName.
        /// </summary>
        public string ErrorMessage 
        { 
            get
            {
                if (UIFieldName == NullValues.NullString)
                { return _errorMessage.Replace("<FieldName>", FieldName); }
                else
                { return _errorMessage.Replace("<FieldName>", UIFieldName); }
            }
            set
            {
                _errorMessage = value;
            }
        }
        private string _errorMessage;


        // Constructors


        public RuleError()
        {
            FieldName = NullValues.NullString;
            UIFieldName = NullValues.NullString;
            SortOrder = int.MaxValue;
            ErrorMessage = NullValues.NullString;
        }


        public RuleError(string fieldName, string errorMessage)
        {
            FieldName = fieldName;
            UIFieldName = NullValues.NullString;
            SortOrder = int.MaxValue;
            ErrorMessage = errorMessage;
        }


        // CompareTo
        // Implementation of IComparable.
        int IComparable.CompareTo(object obj)
        {
            RuleError error2 = (RuleError)obj;
            return SortOrder.CompareTo(error2.SortOrder);
        }
    }
}
